package com.guigu.www.tangyingmeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.www.denghao.service.DHMemberService;
import com.guigu.www.mapper.*;
import com.guigu.www.pojo.*;
import com.guigu.www.tangyingmeng.service.TymBespeakResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
@Service
public class TymBespeakResultServiceImpl implements TymBespeakResultService {

    @Autowired
    private BespeakResultMapper resultMapper;
    @Autowired
    private PersonClassificationMapper personMapper;
    @Autowired
    private HealthManagerMapper healthMapper;
    @Autowired
    private BespeakMapper bespeakMapper;
    @Autowired
    private DHMemberService memberService;
    @Autowired
    private BespeakSetMealMapper setMealMapper;
    @Autowired
    private DiseaseMapper diseaseMapper;
    @Autowired
    private ConstitutionTestMapper constitutionTestMapper;
    @Autowired
    private MindTestMapper mindTestMapper;
    @Autowired
    private RiskTestMapper riskTestMapper;




    @Override
    public PageVo<BespeakResult> selresuleall(Map<String, String> map) {
        QueryWrapper<BespeakResult> wrapper = new QueryWrapper<>();
        wrapper.like("bes_result_id", map.get("findid")).like("bes_result_name", map.get("findname")).eq("bes_result_health",1);
        IPage<BespeakResult> page = new Page<>(Integer.valueOf(map.get("index")), Integer.valueOf(map.get("pageSize")));
        page = resultMapper.selectPage(page, wrapper);
        PageVo<BespeakResult> pageVo = new PageVo<>();
        pageVo.setTotal(page.getTotal());
        pageVo.setRows(page.getRecords());
        for (BespeakResult result:pageVo.getRows()) {
            result.setPerson(personMapper.selectById(result.getPerClassId()));
        }
        return pageVo;
    }

    //查询单个体检结果详情表
    @Override
    public BespeakResult selresultone (Integer id) {
        BespeakResult result = resultMapper.selectById(id);
        result.setBespeak(selbespeak(result.getBesId()));
        result.setMember(selmember(result.getBesId()));
        result.setSetMeal(selsetmeal(result.getBesId()));
        if(result.getBesResultNormal() != 0){
            result.setDisease(seldisease(result.getBesResultNormal()));
        }else {
            result.setDisease(null);
        }
        result.setConstitutionTest(selconstitutionTest(result.getMember().getMemId()));
        result.setMindTest(selmindTest(result.getMember().getMemId()));
        result.setRiskTest(selrisktest(result.getMember().getMemId()));
        return result;
    }

    //确认分类
    @Override
    public Boolean queren(BespeakResult result) {
        result.setBesResultConfirm(1);
        int update = resultMapper.updateById(result);
        if(update>0){
            return true;
        } else {
          return false;
        }
    }

    //体检预约表
    @Override
    public Bespeak selbespeak (Integer id) {
        return bespeakMapper.selectById(id);
    }

    //健康管理师表
    @Override
    public List<HealthManager> selhealthall() {
        QueryWrapper<HealthManager> wrapper = new QueryWrapper<>();
        wrapper.eq("hm_state", 0).eq("role_id",2);
        List<HealthManager> list = healthMapper.selectList(wrapper);
        return list;
    }

    //人群分类表
    @Override
    public List<PersonClassification> selpersonall(){
      return personMapper.selectList(null);
    }

    //会员及其关系表
    @Override
    public Member selmember (Integer id) {
        Bespeak bespeak = bespeakMapper.selectById(id);
        Member member = memberService.queryByMemIdMember(bespeak.getMemId());
        member.setTrends(memberService.queryByMemIdTrends(bespeak.getMemId()));
        member.setCaseHistory(memberService.queryByMemIdCaseHistory(bespeak.getMemId()));
        return member;
    }

    //体检套餐表
    @Override
    public BespeakSetMeal selsetmeal (Integer id) {
        Bespeak bespeak = bespeakMapper.selectById(id);
       return setMealMapper.selectById(bespeak.getBesSmId());
    }

    //疾病表
    @Override
    public Disease seldisease (Integer id) {
       return diseaseMapper.selectById(id);
    }

    //体质测评表
    @Override
    public ConstitutionTest selconstitutionTest (Integer id) {
        QueryWrapper<ConstitutionTest> wrapper = new QueryWrapper<>();
        wrapper.eq("mem_id",id);
        return constitutionTestMapper.selectOne(wrapper);
    }

    //心理测评表
    @Override
    public MindTest selmindTest (Integer id) {
        QueryWrapper<MindTest> wrapper = new QueryWrapper<>();
        wrapper.eq("mem_id",id);
        return mindTestMapper.selectOne(wrapper);
    }
    
    //风险评估表
    @Override
    public RiskTest selrisktest (Integer id) {
        QueryWrapper<RiskTest> wrapper = new QueryWrapper<>();
        wrapper.eq("mem_id",id);
        return riskTestMapper.selectOne(wrapper);
    }

}
